package com.kitty.game.rank.service.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.rank.message.RespTopUser;
import com.kitty.game.rank.model.PersonInfo;
import com.kitty.game.rank.model.record.RankRecord;
import com.kitty.game.rank.model.record.RoleLevelRankRecord;
import com.kitty.game.role.model.RoleSimple;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**角色等级排行榜处理类*/
@Component
public class RoleLevelRankHandler extends RankHandler {
    @Override
    public void init() {
        Dao dao = SpringUtils.getBean(Dao.class);
        List<RoleSimple> roles = dao.query(RoleSimple.class, Cnd.NEW().desc("level"), dao.createPager(1, getRankSize()));
        for (RoleSimple role : roles) {
            if(role.isAi(role)){
                continue;
            }
            this.update(role.getGid(), new RoleLevelRankRecord(role.getGid(), role.getLevel(), role.getPartyName(), role.getName(), role.getPolar(), role.getRoleIcon()));
        }
    }

    public RespTopUser getRankResponse(short type) {
        RespTopUser respTopUser = new RespTopUser();
        respTopUser.setType(type);
        respTopUser.setCookie(2);
        respTopUser.setList(new ArrayList<>());
        List<PersonInfo> personInfoList = getPersonInfoList(getRecordsFrom(0, getRankSize()));
        respTopUser.setList(personInfoList);
        respTopUser.setSize((short) respTopUser.getList().size());
        return respTopUser;
    }

    public List<PersonInfo> getPersonInfoList(List<RankRecord> records) {
        List<PersonInfo> list = new ArrayList<>(records.size());
        for (RankRecord rankRecord : records) {
            RoleLevelRankRecord roleLevelRankRecord = (RoleLevelRankRecord)rankRecord;
            PersonInfo personInfo = new PersonInfo();
            personInfo.setList(new ArrayList<>());
            personInfo.getList().add(new FiedValue(305, roleLevelRankRecord.getGid()));
            personInfo.getList().add(new FiedValue(855, roleLevelRankRecord.getPartyName()));
            personInfo.getList().add(new FiedValue(1, roleLevelRankRecord.getName()));
            personInfo.getList().add(new FiedValue(31, roleLevelRankRecord.getLevel()));
            personInfo.getList().add(new FiedValue(44, roleLevelRankRecord.getPolar()));
            personInfo.getList().add(new FiedValue(40, roleLevelRankRecord.getRoleIcon()));
            list.add(personInfo);
        }
        return list;
    }
}
